/**
  ******************************************************************************
  * @file    p32f031_it.c
  * @author  MCU SYSTEM Team
  * @Version V1.0.0
  * @Date    2020-10-19
  * @brief   Interrupt Service Routines.
  ******************************************************************************

  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

// #include "main.h"
#include "INT.h"
// #include "SPI_Flow.h"
#include "BSP.h"
// #include "uart.h"
#include "mg_api.h"
// extern SPI_HandleTypeDef SpiHandle;
/* Private includes ----------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define APP_Offset 0x1010
#define BOOT_PROGRAM_DATA_START ((uint32_t)0x00000000)
#define APP_ENTRY_ADDRESS (BOOT_PROGRAM_DATA_START + APP_Offset)
/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private user code ---------------------------------------------------------*/

/* External variables --------------------------------------------------------*/

/******************************************************************************/
/*           Cortex-M0+ Processor Interruption and Exception Handlers          */
/******************************************************************************/

/**
 * @brief	This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}
/**
 * @brief This function handles Non maskable interrupt.
 */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x04));
  AppEntry();
  /* USER CODE END NonMaskableInt_IRQn 0 */
}

/**
 * @brief This function handles Hard fault interrupt.
 */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x08));
  AppEntry();
  /* USER CODE END HardFault_IRQn 0 */
}

/**
 * @brief This function handles System service call via SWI instruction.
 */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x2C));
  AppEntry();
  /* USER CODE END SVCall_IRQn 0 */
}

/**
 * @brief This function handles Pendable request for system service.
 */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x38));
  AppEntry();
  /* USER CODE END PendSV_IRQn 0 */
}
/**
 * @brief This function handles System tick timer.
 */
void SysTick_Handler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x3C));
  AppEntry();
}

/******************************************************************************/
/* P32F031 Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_xm32f1xx.s).                    */
/******************************************************************************/

/**
 * @brief This function handles WWDG Interrupt .
 */
void WWDG_IRQHandler(void)
{
  /* USER CODE BEGIN WWDG_IRQn 0 */
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x40));
  AppEntry();
  /* USER CODE END WWDG_IRQn 0 */
}
void PVD_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x44));
  AppEntry();
}
void RTC_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x48));
  AppEntry();
}
void FLASH_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x4C));
  AppEntry();
}
void RCC_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x50));
  AppEntry();
}
void EXTI0_1_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x54));
  AppEntry();
}

void EXTI2_3_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x58));
  AppEntry();
}

void EXTI4_15_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x5C));
  AppEntry();
}

// extern uint8_t Over;
void DMA1_Channel1_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x64));
  AppEntry();
  // DMA1->IFCR |= 0x00000001;
}
void DMA1_Channel2_3_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x68));
  AppEntry();
  // DMA1->IFCR |= 0x00000010;
}

void ADC_COMP_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x70));
  AppEntry();
}
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x74));
  AppEntry();
}
void TIM1_CC_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x78));
  AppEntry();
}

void TIM3_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x80));
  AppEntry();
}
/*void TIM6_LPTIM_IRQHandler(void)
{
}
*/
void LPTIM1_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x84));
  AppEntry();
}

void TIM14_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x8C));
  AppEntry();
}

void TIM16_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x94));
  AppEntry();
}
void TIM17_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x98));
  AppEntry();
}
void I2C1_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0x9C));
  AppEntry();
}
void SPI1_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0xA4));
  AppEntry();
}

/*
void SPI2_IRQHandler(void)
{
    volatile void (*AppEntry)(void); AppEntry = (volatile void(*)())(*(uint32_t*)(APP_ENTRY_ADDRESS+96)); AppEntry();
}
*/
void USART1_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0xAC));
  AppEntry();
}
void USART2_IRQHandler(void)
{
  volatile void (*AppEntry)(void);
  AppEntry = (volatile void (*)())(*(uint32_t *)(APP_ENTRY_ADDRESS + 0xB0));
  AppEntry();
}
